package com.axend.aerosense.dev.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.axend.aerosense.base.fragment.MvvmBaseFragment;
import com.axend.aerosense.base.impl.IComplete;
import com.axend.aerosense.base.impl.ILoadDataResult;
import com.axend.aerosense.base.impl.ILoginStatusChanged;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.base.viewmodel.BaseViewModelFactroy;
import com.axend.aerosense.common.base.IHostLayout;
import com.axend.aerosense.common.base.ITitle;
import com.axend.aerosense.common.bean.MoveBindRoomBean;
import com.axend.aerosense.common.bean.RadarBean;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.ui.LoadingDialog;
import com.axend.aerosense.common.util.FragmentUtil;
import com.axend.aerosense.dev.activity.DevBindRoomActivity;
import com.axend.aerosense.dev.activity.NewDevSetNetActivity;
import com.axend.aerosense.dev.adapter.DevRoomListAdapter;
import com.axend.aerosense.common.bean.BindRoomBean;
import com.axend.aerosense.dev.entity.BindRoomListBean;
import com.axend.aerosense.dev.entity.DeviceBindRoomBean;
import com.axend.aerosense.dev.impl.ITitleCancel;
import com.axend.aerosense.dev.viewmodel.BindRoomViewModel;
import com.axend.aerosense.dev.R;
import com.axend.aerosense.dev.databinding.DevFragmentBindRoomBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ChenHongWei
 * @date :2021/1/12 10:29
 */

public class DevBindRoomFragment
        extends MvvmBaseFragment<DevFragmentBindRoomBinding, BindRoomViewModel>
        implements ILoginStatusChanged, ILoadDataResult<BindRoomListBean>, IComplete {

    private DevRoomListAdapter listAdapter;
    private List<BindRoomBean> list = new ArrayList<>();
    private String radarId;
    private boolean reBind = false;
    private RadarBean radarBean;

    public static DevBindRoomFragment getInstance(){
        DevBindRoomFragment fragment = new DevBindRoomFragment();
//        Bundle bundle = new Bundle();
//        bundle.putString(ARG_PARAM, str);
//        fragment.setArguments(bundle);   //设置参数
        return fragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        setLoadSir(viewDataBinding.roomContentView);
        showLoading();
    }

    private void initView(){
        Bundle bundle = getArguments();
        if (null != bundle){
            radarId = bundle.getString(DevDeviceSettingsFragment.RADAR_ID);
            reBind = bundle.getBoolean(DevDeviceSettingsFragment.RE_BIND, false);
            radarBean = (RadarBean) bundle.getSerializable(DevDeviceSettingsFragment.ARG_PARAM);
        }

        if (!TextUtils.isEmpty(radarId) && getActivity() instanceof NewDevSetNetActivity) {
            ((NewDevSetNetActivity) getActivity()).enableBack(false);
        }

        viewDataBinding.devLinearLayoutAdd.setOnClickListener(v -> {
            //添加按钮
            ARouter.getInstance()
                    .build(RouterActivityPath.Room.PAGE_ROOM_SETTING)
                    .navigation();
        });

        listAdapter = new DevRoomListAdapter(list);
        viewDataBinding.devRoomList.setLayoutManager(new LinearLayoutManager(getContext()));
        viewDataBinding.devRoomList.setAdapter(listAdapter);
        viewModel.getIsComplete().observe(getViewLifecycleOwner(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if (aBoolean){
                    //去下一个界面
                    if (reBind){
                        getActivity().onBackPressed();
                        return;
                    }

                    DevAddSuccessFragment fragment = new DevAddSuccessFragment();
                    FragmentUtil.add(getActivity(), ((IHostLayout)getActivity()).getContentId(), fragment, DevBindRoomFragment.this, true);
                }
            }
        });
        isShowTitleBarRightText(true);
    }

    private void isShowTitleBarRightText(boolean isShow){
        Activity activity = getActivity();
        if (activity instanceof NewDevSetNetActivity){
            NewDevSetNetActivity newDevSetNetActivity = (NewDevSetNetActivity) activity;
            newDevSetNetActivity.isShowRightText(isShow, v->{
                completeSelect();
            });
        }
    }

    /** 完成按钮点击后 */
    public void completeSelect(){
        if (null != listAdapter.getCustomBindRoomBean()){
            if (reBind){
                ArrayList<RadarBean> radarBeans = new ArrayList<>();
                radarBeans.add(radarBean);
                viewModel.reBindRoom(getActivity(), new MoveBindRoomBean(listAdapter.getCustomBindRoomBean().getRoomUuid(), radarBeans));
            }else {
                viewModel.bindRoom(new DeviceBindRoomBean(radarId, listAdapter.getCustomBindRoomBean().getRoomUuid()),getActivity());
            }
        }
    }

    /** 完成按钮点击后 */
    @Override
    public void complete(){
        BindRoomBean bindRoomBean = listAdapter.getCustomBindRoomBean();
        if (null != bindRoomBean){
            Intent intent = new Intent();
            intent.putExtra("ROOM" , bindRoomBean);
            getActivity().setResult(DevBindRoomActivity.BING_RESULT_CODE, intent);
            getActivity().finish();
//            viewModel.devsBindRoom(new DeviceMoveBindRoomBean(rid, listAdapter.getCustomBindRoomBean().getRoomUuid()), getActivity());
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        ((ITitle)getActivity()).setTitle(getString(R.string.dev_bind_room));
        //新建房间后返回到此页面会自动刷新房间列表
        viewModel.onLoad();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        isShowTitleBarRightText(false);
    }

    @Override
    public int getLayoutId() {
        return R.layout.dev_fragment_bind_room;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected BindRoomViewModel getViewModel() {
        BaseViewModelFactroy<BindRoomListBean> myViewModelFactroy = new BaseViewModelFactroy<>(this, this);
        return new ViewModelProvider(this, myViewModelFactroy).get(BindRoomViewModel.class);
    }

    @Override
    protected void onRetryBtnClick() {
        showLoading();
        viewModel.onLoad();
    }

    @Override
    public void loadDataSuccess(BindRoomListBean bindRoomBean) {
        if(null != bindRoomBean && null != bindRoomBean.getRoomList()){
            Activity activity = getActivity();
            boolean show = false;
            NewDevSetNetActivity newDevSetNetActivity = null;
            if (activity instanceof NewDevSetNetActivity){
                newDevSetNetActivity = (NewDevSetNetActivity) activity;
            }
            if (bindRoomBean.getRoomList().size() > 0){
                //更新列表
                listAdapter.upDateList(bindRoomBean.getRoomList());
                show = true;

            }else{
                show = false;
            }
            if(null != newDevSetNetActivity && !isHidden()){
                newDevSetNetActivity.isShowRightText(show, v->{
                    completeSelect();
                });
            }
        }
    }

    @Override
    public void loadDataFail(String decs) {

    }

    @Override
    public void loginStatusChanged() {

    }
}
